<?php
/* SVN FILE: $Id: search.php 515 2007-04-04 23:00:00Z phpnut $ */
/**
 * Short description for file.
 *
 * Long description for file
 *
 * PHP versions 4 and 5
 *
 * Cake Bakery
 *
 * Copyright (c) 2006,	Cake Software Foundation, Inc.
 * 							1785 E. Sahara Avenue, Suite 490-204
 * 							Las Vegas, Nevada 89104
 *
 * Licensed under the CAKE SOFTWARE FOUNDATION LICENSE(CSFL) version 1.0
 * Redistributions of files must retain the above copyright notice.
 * You may not use this file except in compliance with the License.
 *
 * You may obtain a copy of the License at:
 * License page: http://www.cakefoundation.org/licenses/csfl/
 * Copyright page: http://www.cakefoundation.org/copyright/
 *
 * @filesource
 * @copyright		Copyright (c) 2006, Cake Software Foundation, Inc.
 * @link				http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
 * @package			bakery
 * @subpackage		bakery.models
 * @since			Bakery v 1.0.0.0
 * @version			$Revision: 515 $
 * @modifiedby		$LastChangedBy: phpnut $
 * @lastmodified	$Date: 2007-04-05 08:00:00 +0900 (木, 05 4 2007) $
 * @license			http://www.cakefoundation.org/licenses/csfl/  The CSFL License
 */
/**
 * Included libraries.
 *
 */
	if (!class_exists('File')) {
		 uses ('file');
	}
/**
 * Short description for file.
 *
 * Long description for file
 *
 * @package		bakery
 * @subpackage	bakery.models
 * @since		Bakery v 1.0.0.0
 *
 */
class Search extends AppModel {
	var $name = 'Search';
	var $useTable = false;
	var $classes = array();
	var $filePaths = array(TMP);
	var $words = array();
	var $types = array();

	function find($types = array(), $words = array()){
		$this->words = $words;
		$this->types = $types;
		foreach($types as $type){
			if($type === 'File'){
				$this->classes[$type] =& new File(TMP.'files');
			} else {
				$this->classes[$type] =& new $type();
			}
		}
		return $this->__search();
	}

	function __search(){
		foreach ($this->types as $type){
			if($type === 'File'){
			} else {
				$conditions = $this->__buildLike($type);
				$this->classes[$type]->recursive = '-1';
				$results[] = $this->classes[$type]->findAll($conditions);
			}
		}
		return $results;
	}

	function __buildLike($type){
		$info = $this->classes[$type]->loadInfo();
		$db =& ConnectionManager::getDataSource($this->classes[$type]->useDbConfig);
		$first = true;

		foreach ($info as $field){
			foreach($field as $key => $value){
				if($value['type'] === 'string' || $value['type'] === 'text'){
					for($i = 0; $i < count($this->words); $i++){
						$word = $db->value('%'. $this->words[$i] .'%');
						if ($i === 0){
							$like = '('.$this->classes[$type]->name . '.' .$value['name'] . ' LIKE ' . $word. ')';
						} else {
							$like .= ' OR ('.$this->classes[$type]->name . '.' .$value['name'] . ' LIKE ' . $word. ')';
						}
					}
					if($first === true){
						$conditions = $like;
						$first = false;
					} else {
						$conditions .= ' OR ' . $like;
					}
					unset($like);
				}
			}
		}
		return $conditions;
	}
}
?>